package com.ruoyi.web.controller.production;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.production.domain.vo.ProductionWorkOrderAndPlanVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.production.domain.ProductionWorkOrder;
import com.ruoyi.production.service.IProductionWorkOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生产工单Controller
 *
 * @author jgb
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/production/order")
public class ProductionWorkOrderController extends BaseController
{
    @Autowired
    private IProductionWorkOrderService productionWorkOrderService;

    /**
     * 查询生产工单详细产品以及内容
     */
    @PreAuthorize("@ss.hasPermi('production:order:query')")
    @GetMapping("/getOrder/{id}")
    public AjaxResult getOrder(@PathVariable("id") Long id)
    {
        ProductionWorkOrderAndPlanVo list = productionWorkOrderService.getOrderById(id);
        System.out.println("list = " + list);
        return success(list);
    }

    /**
     * 查询生产工单列表
     */
    @PreAuthorize("@ss.hasPermi('production:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductionWorkOrderAndPlanVo productionWorkOrder)
    {
        startPage();
        List<ProductionWorkOrderAndPlanVo> list = productionWorkOrderService.selectProductionWorkOrderAndPlan(productionWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出生产工单列表
     */
    @PreAuthorize("@ss.hasPermi('production:order:export')")
    @Log(title = "生产工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductionWorkOrder productionWorkOrder)
    {
        List<ProductionWorkOrder> list = productionWorkOrderService.selectProductionWorkOrderList(productionWorkOrder);
        ExcelUtil<ProductionWorkOrder> util = new ExcelUtil<ProductionWorkOrder>(ProductionWorkOrder.class);
        util.exportExcel(response, list, "生产工单数据");
    }

    /**
     * 获取生产工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productionWorkOrderService.selectProductionWorkOrderById(id));
    }

    /**
     * 新增生产工单
     */
    @PreAuthorize("@ss.hasPermi('production:order:add')")
    @Log(title = "生产工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductionWorkOrderAndPlanVo productionWorkOrderAndPlanVo)
    {
        return toAjax(productionWorkOrderService.insertProductionWorkOrder(productionWorkOrderAndPlanVo));
    }

    /**
     * 修改生产工单
     */
    @PreAuthorize("@ss.hasPermi('production:order:edit')")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductionWorkOrderAndPlanVo productionWorkOrderAndPlanVo)
    {
        return toAjax(productionWorkOrderService.updateProductionWorkOrderAndDetail(productionWorkOrderAndPlanVo));
    }

    /**
     * 修改生产工单状态以及计划状态（生产中）
     */
    @PreAuthorize("@ss.hasPermi('production:order:edit')")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PutMapping("/editStatus")
    public AjaxResult editStatus(@RequestBody ProductionWorkOrder productionWorkOrder)
    {
        return toAjax(productionWorkOrderService.updateProductionWorkOrderStatus(productionWorkOrder));
    }

    /**
     * 删除生产工单
     */
    @PreAuthorize("@ss.hasPermi('production:order:remove')")
    @Log(title = "生产工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        return toAjax(productionWorkOrderService.deleteProductionWorkOrderByIds(ids));
    }
}
